import gift from '@/components/gift'
import alertBindPhone from '@/views/alert_bind_phone/index'
import {roomInitRoomChat,roomInitRoomVideo} from '@/api/room'
import {mapGetters} from 'vuex'
import ChatDisplay from '@/utils/xchat/chatDisplay'
import XMessager from '@/utils/xMessager/xMessager'
import Xchat from '@/utils/xMessager/chat/Xchat'
import XchatH5Player from '@/utils/xMessager/player/mobile/fullScreenPlayer'
import {Base64} from '@/utils/xchat/base64'
import {formatWatchServer} from '@/utils'
import {MSG_LOGIN_ACK,MSG_REENTER,MSG_SESSION_PARAM,MSG_SOMEBODY_IN,MSG_SOMEBODY_OUT,MSG_ROOMHOT,MSG_SOMEBODY_CHATENABLE,MSG_KICKOUT,MSG_TOKEN_INFO,MSG_TOKEN_REQUEST,MSG_ROOM_REINIT,MSG_SOCKET_ERROR} from '@/utils/xchatMessageHandle'
import {getRoomPassword} from '@/utils/auth'
export default{
	inject:['skeletonLoad'],
	data(){
		return{
			height:undefined,
			fullScreenPlayer: null,
			xmessager:null,
			chatDisplay:null,
			chat:null,
			searchQuery:{
				room_id:this.room_id,
				eng_name:this.eng_name,
				room_password:undefined
			},
			loginDialog:false,
			loginTimer:null,
			playerLoading:false,
			watchServer:[]
		}
	},
	props:{
		room_id:{
			type:String
		},
		eng_name:{
			type:String
		}
	},
	computed:{
		...mapGetters([
		  'user',
			'token',
			'roomLive',
			'micLoading',
			'micIndex',
			'roompara',
			'micLoadingTimer'
		])
	},
	components:{
		gift,
		alertBindPhone
	},
	created(){
		this.skeletonLoad()
		if("login_alert_time" in this.roompara && this.roompara.login_alert_time !== 0){
			this.loginTimer = setInterval(()=>{
				this.loginDialog = true
			},this.roompara.login_alert_time * 1000 * 60)
		}
		if(getRoomPassword(`${this.$route.params.roomid}-${this.user.uid}`)){
			this.searchQuery.room_password = getRoomPassword(`${this.$route.params.roomid}-${this.user.uid}`)
		}
		this.playerLoading = true
		Promise.all([roomInitRoomChat(this.searchQuery),roomInitRoomVideo(this.searchQuery)]).then(result=>{
			let wsres = result[0]
			let videores = result[1]
			this.playerLoading = false
			//聊天start
			let userpara = JSON.parse(Base64.decode(wsres.data.appdata))
			let ws_param = {
				ip : wsres.data.chat_server_ip,		//"192.168.0.120",
				port : 19288,	//roompara.serverpo
				uid : parseInt(wsres.data.uid),
				room_id : wsres.data.room_id,
				level : wsres.data.levelinroom,
				token : wsres.data.chat_token,
				nickname : Base64.decode(userpara.nickname_b64),
				chatdisable : 0,
				appdata :Base64.encode(JSON.stringify(userpara)),
				wss: userpara.wss,
			}
			this.$store.commit("SET_WSPARAM",ws_param)
			videores.data.is_live = videores.data.is_live && videores.data.watch_server
			this.$store.commit('SET_ROOMLIVE',videores.data.is_live)
			this.$store.commit('SET_ROOMCHANGE',!videores.data.is_live)
			this.xmessager = new XMessager(ws_param)
			//聊天服务器
			new Xchat(ws_param).then(chat=>{
				this.chat = chat
				this.$store.commit('SET_WS',this.chat.websocket.ws)
				this.chatEvents()
				this.commonChatEvents()
			})
			//聊天end
			this.$nextTick(()=>{
				if(videores.data.watch_server && videores.data.is_live){
					if(("login_alert_time" in this.roompara && this.roompara.login_alert_time !== 0) || this.user.logined){
						this.fullScreenPlayer = this.initPlayer(videores.data.watch_server)
						this.playerEvents()
					}
				}
				this.chatDisplay = new ChatDisplay(this.$refs.pubChatWrap,this.xmessager,this.mode,50)
			})

		}).catch(err=>{
			this.playerLoading = false
			console.log(err)
		})
	},
	methods:{
		initPlayer(watchServer,openHls = false,serverDecode = true){
			this.watchServer = watchServer
			//销毁播放器重新初始化播放器后不需要在解码
			if(serverDecode){
				watchServer = formatWatchServer(watchServer,Base64)
			}
			let video_param = {
				el:'fullscreen-player-wrap',
				watchServer:watchServer,
				mode:this.mode,
				openHls:openHls
			}
			return new XchatH5Player(video_param)
		},
		destroyPlayer(){
			if(this.fullScreenPlayer){
				this.fullScreenPlayer.destroy()
			}
			this.fullScreenPlayer = null
		},
		setPrivateChatArray(private_chat_array){
			if(this.xmessager.level < 500){
				private_chat_array = private_chat_array.filter((item)=>{
					return (item.uid != this.xmessager.uid && item.level >= 500)
				})
			}else{
				private_chat_array = private_chat_array.filter((item)=>{
					return (item.uid != this.xmessager.uid && item.rank > 100)
				})
			}
			this.$store.commit('SET_PRIVATECHATARRAY',private_chat_array)
		},
		commonChatEvents(){
			this.chat.on(this.chat.chatEvents.MSG_LOGIN_ACK,(param)=>{
				MSG_LOGIN_ACK(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay})
			})
			this.chat.on(this.chat.chatEvents.MSG_REENTER,(param)=>{
				MSG_REENTER(param,{xmessager:this.xmessager,player:this.fullScreenPlayer,layer:this.$layer})
			})
			this.chat.on(this.chat.chatEvents.MSG_SESSION_PARAM,(param)=>{
				MSG_SESSION_PARAM(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay})
			})
			this.chat.on(this.chat.chatEvents.MSG_SOMEBODY_IN,({appdata,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
				MSG_SOMEBODY_IN(appdata,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,roompara:this.roompara})
			})
			this.chat.on(this.chat.chatEvents.MSG_SOMEBODY_OUT,({param,appdata,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
				MSG_SOMEBODY_OUT(param,appdata,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,roompara:this.roompara})
			})
			this.chat.on(this.chat.chatEvents.MSG_SOMEBODY_CHANGELEVEL,({param,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
			})
			this.chat.on(this.chat.chatEvents.MSG_SOMEBODY_CHANGEAPPDATA,({param,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
			})
			this.chat.on(this.chat.chatEvents.MSG_ROOMHOT,(param)=>{
				MSG_ROOMHOT(param,this.$store)
			})
			this.chat.on(this.chat.chatEvents.MSG_SOMEBODY_CHATENABLE,(param)=>{
				MSG_SOMEBODY_CHATENABLE(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,chat:this.chat})
			})
			this.chat.on(this.chat.chatEvents.MSG_KICKOUT,(param)=>{
				MSG_KICKOUT(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,chat:this.chat})
			})
			this.chat.on(this.chat.chatEvents.MSG_TOKEN_INFO,(param)=>{
				MSG_TOKEN_INFO(param,{chat:this.chat,player:this.fullScreenPlayer,xmessager:this.xmessager,store:this.$store,chatDisplay:this.chatDisplay})
			})
			this.chat.on(this.chat.chatEvents.MSG_TOKEN_REQUEST,(param)=>{
				MSG_TOKEN_REQUEST(param,{chat:this.chat,chatDisplay:this.chatDisplay})
			})
			this.chat.on(this.chat.chatEvents.MSG_ROOM_REINIT,(param)=>{
				MSG_ROOM_REINIT()
			})
			this.chat.on(this.chat.chatEvents.MSG_SOCKET_ERROR,()=>{
				MSG_SOCKET_ERROR(this.chatDisplay)
			})
		},
		offCommonChatEvents(){
			this.chat.off(this.chat.chatEvents.MSG_LOGIN_ACK,(param)=>{
				MSG_LOGIN_ACK(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay})
			})
			this.chat.off(this.chat.chatEvents.MSG_REENTER,(param)=>{
				MSG_REENTER(param,{xmessager:this.xmessager,player:this.fullScreenPlayer,layer:this.$layer})
			})
			this.chat.off(this.chat.chatEvents.MSG_SESSION_PARAM,(param)=>{
				MSG_SESSION_PARAM(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay})
			})
			this.chat.off(this.chat.chatEvents.MSG_SOMEBODY_IN,({appdata,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
				MSG_SOMEBODY_IN(appdata,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,roompara:this.roompara})
			})
			this.chat.off(this.chat.chatEvents.MSG_SOMEBODY_OUT,({param,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
				MSG_SOMEBODY_OUT(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,roompara:this.roompara})
			})
			this.chat.off(this.chat.chatEvents.MSG_SOMEBODY_CHANGELEVEL,({param,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
			})
			this.chat.off(this.chat.chatEvents.MSG_SOMEBODY_CHANGEAPPDATA,({param,private_chat_array})=>{
				this.setPrivateChatArray(private_chat_array)
			})
			this.chat.off(this.chat.chatEvents.MSG_ROOMHOT,(param)=>{
				MSG_ROOMHOT(param,this.$store)
			})
			this.chat.off(this.chat.chatEvents.MSG_SOMEBODY_CHATENABLE,(param)=>{
				MSG_SOMEBODY_CHATENABLE(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,chat:this.chat})
			})
			this.chat.off(this.chat.chatEvents.MSG_KICKOUT,(param)=>{
				MSG_KICKOUT(param,{xmessager:this.xmessager,chatDisplay:this.chatDisplay,chat:this.chat})
			})
			this.chat.off(this.chat.chatEvents.MSG_TOKEN_INFO,(param)=>{
				MSG_TOKEN_INFO(param,{chat:this.chat,player:this.fullScreenPlayer,xmessager:this.xmessager,store:this.$store,chatDisplay:this.chatDisplay})
			})
			this.chat.off(this.chat.chatEvents.MSG_TOKEN_REQUEST,(param)=>{
				MSG_TOKEN_REQUEST(param,{chat:this.chat,chatDisplay:this.chatDisplay})
			})
			this.chat.off(this.chat.chatEvents.MSG_ROOM_REINIT,(param)=>{
				MSG_ROOM_REINIT()
			})
			this.chat.off(this.chat.chatEvents.MSG_SOCKET_ERROR,()=>{
				MSG_SOCKET_ERROR(this.chatDisplay)
			})
		}
	},
	mounted(){
		this.height = window.innerHeight
	},
	destroyed(){
		if(this.chat){
			if(this.chat.websocket && this.chat.websocket.xchat_protocol.logined){
				this.chat.websocket.logout()
			}
			this.offCommonChatEvents()
		}
		if(this.micLoadingTimer){
			console.log('micLoadingTimer:'+this.micLoadingTimer)
			this.$store.commit('SET_MICLOADING', false)
			this.$store.commit('SET_MICINDEX', 0)
			clearInterval(this.micLoadingTimer)
			this.$store.commit('SET_MICLOADING_TIMER', null)
		}
	}
}